package GUILayer;
import ControlLayer.CtrProductAttribute;
import ModelLayer.ProductAttribute;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;

public class GUIProductAttribute extends javax.swing.JFrame {
    private CtrProductAttribute ctrProductAttribute;

    public GUIProductAttribute() {
        initComponents();
        ctrProductAttribute = CtrProductAttribute.getInstance();
        loadTableContent();
    }

    private void loadTableContent() {
        ((DefaultTableModel)tblProductAttributeList.getModel()).setRowCount(0);
        ArrayList<ProductAttribute> productAttributeList = ctrProductAttribute.getProductAttributeList();
        for (ProductAttribute productAttribute : productAttributeList) {
            String attributeIDs = new String();
            for (Integer attributeID : productAttribute.getAttributeIDs()) {
                attributeIDs += attributeID + ", ";
            }
            ((DefaultTableModel)tblProductAttributeList.getModel()).addRow(
                new Object [] {
                    productAttribute.getIDProductAttribute(),
                    productAttribute.getIDProduct(),
                    productAttribute.getReference(),
                    productAttribute.getSupplierReference(),
                    productAttribute.getLocation(),
                    productAttribute.getEan13(),
                    productAttribute.getWholesalePrice(),
                    productAttribute.getPrice(),
                    productAttribute.getEcotax(),
                    productAttribute.getQuantity(),
                    productAttribute.getWeight(),
                    productAttribute.getDefaultOn(),
                    attributeIDs
                }
            );
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlProductAttributeList = new javax.swing.JScrollPane();
        tblProductAttributeList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblProductAttributeList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "id_product", "reference", "supplier_reference", "location", "ean13", "wholesale_price", "price", "ecotax", "quantity", "weight", "default_on", "attribute IDs"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class, java.lang.Float.class, java.lang.Float.class, java.lang.Integer.class, java.lang.Float.class, java.lang.Boolean.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrlProductAttributeList.setViewportView(tblProductAttributeList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlProductAttributeList, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlProductAttributeList, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIProductAttribute().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrlProductAttributeList;
    private javax.swing.JTable tblProductAttributeList;
    // End of variables declaration//GEN-END:variables

}
